package com.tensquare.spit.controller;

import com.tensquare.entity.PageResult;
import com.tensquare.entity.Result;
import com.tensquare.entity.StatusCode;
import com.tensquare.spit.pojo.Spit;
import com.tensquare.spit.service.SpitService;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/spit")
@CrossOrigin
public class SpitController {
    @Autowired
    private SpitService spitService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * Spit带复杂条件分页
     *
     * @param page
     * @param size
     * @param spit
     * @return
     */
    @PostMapping("/search/{page}/{size}")
    public Result searchPage(@PathVariable int page, @PathVariable int size,
                             @RequestBody Spit spit) {
        return new Result(true, StatusCode.OK, "查询成功",
                spitService.searchPage(spit, page, size));
    }

    /**
     * 根据条件查询Spit列表
     */
    @PostMapping("/search")
    public Result searchByQuery(@RequestBody Spit spit) {
        return new Result(true, StatusCode.OK, "查询成功",
                spitService.searchByQuery(spit));
    }

    /**
     * 吐槽点赞
     *
     * @param spitId
     * @return
     */
    @RequestMapping(value = "/thumbup/{spitId}", method = RequestMethod.PUT)
    public Result thumbup(@PathVariable String spitId) {
        //从登录信息中获取到用户id来判断是否重复点赞
        String userId = "111";
        if (null != redisTemplate.opsForValue().get("thumbup_" + userId)) {
            return new Result(false, StatusCode.PER_ERROR, "不能重复点赞");
        }
        spitService.updateThumbup(spitId);
        redisTemplate.opsForValue().set("thumbup_" + userId, 1);
        return new Result(true, StatusCode.OK, "点赞成功");
    }

    /**
     * 根据上级ID查询吐槽数据
     *
     * @param parentid
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/comment/{parentid}/{page}/{size}")
    public Result findByParentid(@PathVariable String parentid,
                                 @PathVariable int page, @PathVariable int size) {
        Page<Spit> spitPage = spitService.findByParentid(parentid, page, size);
        return new Result(true, StatusCode.OK, "查询成功",
                new PageResult<Spit>(spitPage.getTotalElements(), spitPage.getContent()));
    }

    /**
     * Spit全部列表
     */
    @GetMapping
    public Result findAll() {
        return new Result(true, StatusCode.OK, "查询成功",
                spitService.findAll());
    }

    /**
     * 根据ID查询吐槽
     */
    @GetMapping("/{spitId}")
    public Result findById(@PathVariable String spitId) {
        return new Result(true, StatusCode.OK, "查询成功",
                spitService.findById(spitId));
    }

    /**
     * 增加吐槽
     */
    @PostMapping
    public Result addSpit(@RequestBody Spit spit, HttpServletRequest request) {
        Claims claims = (Claims) request.getAttribute("Authorization_USER");
        if (StringUtils.isEmpty(claims)) {
            return new Result(false, StatusCode.ACCESS_ERROR, "权限不足");
        }
        spit.setUserid(claims.getId());
        spit.setNickname(claims.getSubject());
        spitService.save(spit);
        return new Result(true, StatusCode.OK, "吐槽成功");
    }

    /**
     * 修改吐槽
     */
    @RequestMapping(value = "/{spitId}", method = RequestMethod.PUT)
    public Result findAll(@PathVariable String spitId, @RequestBody Spit spit) {
        spit.set_id(spitId);
        spitService.update(spit);
        return new Result(true, StatusCode.OK, "修改成功");
    }

    /**
     * 根据ID删除吐槽
     */
    @RequestMapping(value = "/{spitId}", method = RequestMethod.DELETE)
    public Result findAll(@PathVariable String spitId) {
        spitService.deleteById(spitId);
        return new Result(true, StatusCode.OK, "删除成功");
    }
}
